using behaviac;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 自定义Behaviac文件管理器
    /// </summary>
    public class CustomBehaviacLoader : FileManager
    {
        private static CustomBehaviacLoader _instance;

        /// <summary>
        /// 将behaviac.FileManager的单例替换为该类
        /// </summary>
        public static void CoverLoader()
        {
            _instance ??= new CustomBehaviacLoader();//父类构造函数会把this赋予静态变量ms_instance，所以最后构造的FileManager会成为FileManager.Instance的实际对象。
            _instance.OnEnable();
        }

        private void OnEnable()
        {

        }

        /// <summary>
        /// 还原默认loader
        /// </summary>
        public static void RecoverLoader()
        {
            _instance?.OnDisable();
        }

        public override byte[] FileOpen(string filePath, string ext)
        {
            string finalPath = Combine(filePath, ext);
            return BehaviacGlobal.Instance.GetBtContent(finalPath);
        }

        public override void FileClose(string filePath, string ext, byte[] fileHandle)
        {

        }

        /// <summary>
        /// 拼接path和ext
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="ext">前面已经包含句号（.）</param>
        /// <returns></returns>
        private string Combine(string filePath, string ext)
        {
            string finalPath = null;
            if (ext == ".bson")
            {
                finalPath = $"{filePath}{ext}.bytes";
            }
            else
            {
                finalPath = $"{filePath}{ext}";
            }
            return finalPath;
        }

        private void OnDisable()
        {

        }
    }
}
